package cn.share.life.system.util.excelMath;

import lombok.Data;
import lombok.ToString;

import java.io.Serializable;

@ToString
@Data
public class MathResultOtherEntity extends MathResultEntity implements Serializable {

    //    现金流入
    public double getCashInflow() {
        return getSalesRevenue() + getRecoveryWorkingCapital() + getRecoveryFixedAssetResidualValue() + getOtherIncomes();
    }

    //    销售收入
    public double getSalesRevenue() {
        return getTransmissionCapacity() * getCapacityPrice() + getTransmissionElectricity() * getTransmissionPrice() * 10;
    }

    //    输电容量
    public double getTransmissionCapacity() {
        return getBaseEntity().getTransmissionCapacity();
    }
    //    容量电价
    public double getCapacityPrice() {
        return getBaseEntity().getCapacityPrice();
    }
    //    输电电量
    public double getTransmissionElectricity() {
        return getBaseEntity().getTransmissionElectricity();
    }
    //    输电电价
    public double getTransmissionPrice() {
        return getBaseEntity().getTransmissionPrice();
    }
    //    现金流出
    public double getCashOutflow() {
        // =F27+F28+F29+SUM(F31:F35)
        return getCapitalInvestment() + getSalesTaxesAndSurcharges() + getOperatingCosts() +
                getPrincipalRepayment() + getInterestPayment() + getIncomeTax() + getEmployeeWelfareFund() + getOtherExpenses();
    }
    //    销售税金及附加
    public double getSalesTaxesAndSurcharges() {
        // =F18/(1+$I$12*(1+$I$13))*$I$12*(1+$I$13)
        return getSalesRevenue() /(1 + getBaseEntity().getValueAddedTaxRate() * (1 + getBaseEntity().getTaxSurchargeRate()))
                * getBaseEntity().getValueAddedTaxRate() * (1 + getBaseEntity().getTaxSurchargeRate());
    }
    //    经营成本
    public double getOperatingCosts() {
        return getBaseEntity().getTotalOperatingCost2();
    }
    //    折旧费用
    public double getDepreciationExpense() {
        // IF(F16<=($L$12),$F$5*$I$6,IF(F16=($L$12+1),(($F$5+$L$11)*95%-E43)/($F$13-$L$12),E30))
        if (getYear() <= getBaseEntity().getInvestmentYear()) {
            // 年份小于等于折旧年限
            return getBaseEntity().getTotalInvestment() * getBaseEntity().getDepreciationRate();
        } else if (getYear() == getBaseEntity().getInvestmentYear() + 1) {
            // 年份等于折旧年限加1
            return ((getBaseEntity().getTotalInvestment() + getBaseEntity().getSubsequentInvestment()) * 0.95 - getPrefEntity().getCumulativeDepreciation())
                    / (getBaseEntity().getDepreciationYears() - getBaseEntity().getInvestmentYear());
        } else {
            // 其他年份
            return getPrefEntity().getDepreciationExpense();
        }
    }
    //    本金偿还
    public double getPrincipalRepayment() {
        // IF($F$9>=F16,$F$7/$F$9,0)
        return getBaseEntity().getRemainingLoanYears() >= getYear() ? (getBaseEntity().getLongTermLoanBalance() / getBaseEntity().getRemainingLoanYears()) : 0.0;
    }
    //    利息支付
    public double getInterestPayment() {
        double sum = getBaseEntity().getLongTermLoanBalance() - (getBaseEntity().getLongTermLoanBalance() / getBaseEntity().getRemainingLoanYears()) * getPrefEntity().getYear();
        // =IF(($F$7-($F$7/$F$9)*E16)>0,($F$7-($F$7/$F$9)*E16)*$F$8,0)
        return sum > 0 ? (sum * getBaseEntity().getLongTermLoanRate()) : 0.0;
    }
    //    所得税
    public double getIncomeTax() {
        // =IF((F18-F28-F29-F30-F32)>0,(F18-F28-F29-F30-F32)*25%,0)
        return getTotalProfit() > 0 ? getTotalProfit() * 0.25 : 0.0;
    }
    //    利润总额
    public double getTotalProfit() {
        // =F18-F28-F29-F30-F32
        return getSalesRevenue() - getSalesTaxesAndSurcharges() - getOperatingCosts() - getDepreciationExpense() - getInterestPayment();
    }
    //    净现金流量
    public double getNetCashFlow() {
        return getCashInflow() - getCashOutflow();
    }
    //    累计净现金流量
    public double getCumulativeNetCashFlow() {
        return getPrefEntity().getCumulativeNetCashFlow() + getNetCashFlow();
    }
    //    净现金流量现值
    public double getNetPresentValue() {
        // =F37/(1+$I$9)^F16
        return getNetCashFlow() / Math.pow(1 + getBaseEntity().getCapitalReturnRate(), getYear());
    }
    //    累计净现金流量现值
    public double getCumulativeNetPresentValue() {
        return getPrefEntity().getCumulativeNetPresentValue() + getNetPresentValue();
    }
    // 累计折旧
    public double getCumulativeDepreciation() {
        return getPrefEntity().getCumulativeDepreciation() + getDepreciationExpense();
    }

    //    回收固定资产余值
    public double getRecoveryFixedAssetResidualValue() {
        if (getYear() == 35) {
            // =($F$5+$L$11)*5%
            return (getBaseEntity().getTotalInvestment() + getBaseEntity().getSubsequentInvestment()) * 0.05;
        }
        return 0.0;
    }
}
